$(window).load(function () {
	$(window).resize(function(){
		addControlZoombar();
	});

	var map = getInitializedMap(lonlat);

	var wms_layers = map.getLayersByClass('OpenLayers.Layer.WMS');
	var wms = wms_layers[0];

	var wmsRequestStringOptions = {
	        REQUEST: "GetFeatureInfo",
	        EXCEPTIONS: "application/vnd.ogc.se_xml",
	        BBOX: wms.map.getExtent().toBBOX(),
	        X: 0,
	        Y: 0,
			INFO_FORMAT: 'text/plain', // 'text/plain', 'application/vnd.ogc.gml',
	        QUERY_LAYERS: 'umanji:building', // wms.params.LAYERS, 다 가져올 경우
	        WIDTH: wms.map.size.w,
	        HEIGHT: wms.map.size.h,
	        FORMAT: 'rss'}

	// 드래그 레이어 등록 및 이벤트 처리 
    var dragFeature = new OpenLayers.Control.DragFeature(layer);
    dragFeature.onComplete = function(layer, pixel) {
    	lonlat = map.getLonLatFromViewPortPx(pixel);
    	$("#lat").val(lonlat.lon);
    	$("#lon").val(lonlat.lat);

		var popup = new OpenLayers.Popup.FramedCloud("umanji", map.getLonLatFromPixel(pixel), null, 
														'이 지점에 등록합니다.', null, true );
		popup.panMapIfOutOfView = true;
		popup.closeOnMove = true;
		map.addPopup(popup, true);

		layer.removeFeatures(layer.features);

		layer.addFeatures( [ new OpenLayers.Feature.Vector (
				new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
		) ] );

        wmsRequestStringOptions['BBOX'] = wms.map.getExtent().toBBOX();
        wmsRequestStringOptions['WIDTH'] = wms.map.size.w;
        wmsRequestStringOptions['HEIGHT'] = wms.map.size.h;
    	wmsRequestStringOptions['X'] = pixel.x;
        wmsRequestStringOptions['Y'] = pixel.y;
        var url =  wms.getFullRequestString(wmsRequestStringOptions);

    };
	map.addControl(dragFeature);
    dragFeature.activate();

	// addControlZoombar();

	// 마우스 클릭 시 발생 이벤트 처리
    map.events.register('click', map, function (e) {
    	lonlat = map.getLonLatFromViewPortPx(e.xy);
    	$("#lat").val(lonlat.lon);
    	$("#lon").val(lonlat.lat);

		var popup = new OpenLayers.Popup.FramedCloud("umanji", map.getLonLatFromPixel(e.xy), null, 
														'이 지점에 등록합니다.', null, true );
		popup.panMapIfOutOfView = true;
		popup.closeOnMove = true;
		map.addPopup(popup, true);

		layer.removeFeatures(layer.features);

		layer.addFeatures( [ new OpenLayers.Feature.Vector (
				new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
		) ] );

        wmsRequestStringOptions['BBOX'] = wms.map.getExtent().toBBOX();
        wmsRequestStringOptions['WIDTH'] = wms.map.size.w;
        wmsRequestStringOptions['HEIGHT'] = wms.map.size.h;
        wmsRequestStringOptions['X'] = e.xy.x;
        wmsRequestStringOptions['Y'] = e.xy.y;
        var url =  wms.getFullRequestString(wmsRequestStringOptions);
    	// cross domain 문제로 인해 YQL로 대체함 2010년 4월 16일
    	// OpenLayers.loadURL(url, '', this, setHTML);

        OpenLayers.Event.stop(e);
    });
});